package com.ecp.commons.filter;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;

public class ExtensionAccessFilter implements Filter {

	//String[]	excludes	= null;
	List<String>	excludes	= null;

	@Override
	public void destroy() {
	}

	@Override
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException,
			ServletException {
		HttpServletRequest httpRequest = (HttpServletRequest) request;
		String requestUri = httpRequest.getRequestURI().toLowerCase();
		String ext = requestUri.substring(requestUri.lastIndexOf(".") + 1);
		if (excludes.contains(ext)) {
			return;
		}
		chain.doFilter(request, response);
	}

	@Override
	public void init(FilterConfig filterConfig) throws ServletException {
		String excludesStr = filterConfig.getInitParameter("excludes");
		excludes = Arrays.asList(excludesStr.split(","));
	}

}
